#!/usr/bin/env python

#############################
# Author: xjonein
#############################

import os, string, subprocess
import getopt, sys

from subprocess import call

preTextInscpection = """
\\\\
|| Checker || Status || Comment || Time spent ||
| | NS | | |
| | NS | | |
| | NS | | |
| | NS | | |
&nbsp;Status:&nbsp; NS=Not started
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; D=Done (Done with review and all review remarks added to list below).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WNBD = Will Never Be Done, Lack of time or interest to do review.

\\\\
&nbsp;Review info:
"""

postTextInscpection = """
\\\\
|| Coverity tested&nbsp; || Result ||
| | |

&nbsp;Review summary:
|| File no || Location || Class || Checker || Description || Action C/R || Checked Y/N ||
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
Class:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ma=Major, Mi=Minor, I=Improvement, C=Comment (not a fault), Q=Question of Intents
Action:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C=Corrected, R=Rejected
Checked:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Review comment checked after rework, Y=Yes, N=No
\\\\
"""

def printWikiInfo(branchName, userSignum):

    p1 = subprocess.Popen("cleartool find -all -element 'brtype(" + branchName + ")' -print -nxname | xargs cleartool ls -d", shell=True, stdout=subprocess.PIPE)
    output = p1.communicate()[0]

    strOutput = output.decode("utf-8")
    arr = strOutput.split("\n")
    arr = arr[0:len(arr)-1] #remove last \n

    ## split version and filename
    for i in range(len(arr)):
        arr[i] = arr[i].split("@@")

    ## remove clearcase rule
    for index in range(len(arr)):
        arr[index][1] = arr[index][1].partition(" ")[0]

    inspectionText = """
----
| *&nbsp;*{*}Author:* | """ + userSignum + """ |
| *Inspection must be finished date:* | |
| *Reason for inspection (CR/TR/other):* | other |
"""
    finalString = inspectionText + preTextInscpection

    startString = "|| File no || File Name || Revision (branch+element) or diff command ||\n"
    fileString = startString


    for index in range(len(arr)):
        fileString = fileString  + "| " + str(index+1) + " | " + arr[index][0] + " | " + arr[index][1] + " | \n"

    finalString = finalString + fileString + postTextInscpection

    print(finalString)

def printUsage():
    print ("Usage: createwikireview <branchname> <usersignum>")


def main(argv):
    
  try:
    branch = argv[0]
    signum = argv[1]
  except:
    printUsage()

  printWikiInfo(branch, signum)
    

## If this is the main script, execute
if(__name__ == "__main__"):
    main(sys.argv[1:]) ## sends all but the scriptname as argument
